package top.hkyzf.study.leetcode.array;

import org.junit.Test;

import java.util.Arrays;
import java.util.TreeMap;

/**
 * description: 
 * @author zhufeng
 * @date 2023/4/25 0025 15:21
 * @version V1.0
 */
public class 按身高排序_2418 {

    @Test
    public void testSortPeople () {
        String[] names = {"Mary","John","Emma"};
        int[] heights = {180,165,170};
        System.out.println(Arrays.toString(sortPeople1(names, heights)));
    }

    public String[] sortPeople(String[] names, int[] heights) {
        TreeMap<Integer, String> treeMap = new TreeMap<>((i1, i2)-> i2-i1);
        for (int i = 0; i < heights.length; i++) {
            treeMap.put(heights[i], names[i]);
        }
        return treeMap.values().toArray(new String[0]);
    }

    public String[] sortPeople1(String[] names, int[] heights) {
        int n = names.length;
        Integer[] indices = new Integer[n];
        for (int i = 0; i < n; i++) {
            indices[i] = i;
        }
        Arrays.sort(indices, (a, b) -> heights[b] - heights[a]);
        String[] res = new String[n];
        for (int i = 0; i < n; i++) {
            res[i] = names[indices[i]];
        }
        return res;
    }

}
